package com.ttl.bc.ap.invoice.bo;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.DynaActionForm;

import com.ttl.common.db.DBUtil;
import com.ttl.common.util.CommonUtils;

public class BCAPInvoiceReportBO {

	@SuppressWarnings({ "unchecked", "deprecation" })
	public void showPettyCashGroupReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 allocateCode = mForm.get("allocateCode").toString();
		String	 ttl 		  = mForm.get("ttl").toString();
		String   billType	  = new String( mForm.get("billType").toString().getBytes("iso8859_1"),"UTF-8");		

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
						
		HashMap hashMap = new HashMap();
	    
		hashMap.put("ttl",ttl);    
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    	    
		hashMap.put("startDateH",(df.format(sDate2).toString()));
		hashMap.put("endDateH",(df.format(eDate2).toString()));	    
		hashMap.put("allocateCode",allocateCode);
		hashMap.put("billType",billType.substring(0, 1));
		hashMap.put("billTypeName",billType);
		
		System.out.println("ttl="+ttl);
		System.out.println("startDate="+sDate);
		System.out.println("endDate="+eDate);
		System.out.println("startDateH="+(df.format(sDate2).toString()));
		System.out.println("endDateH="+(df.format(eDate2).toString()));	    
		System.out.println("allocateCode="+allocateCode);
		System.out.println("billType="+billType.substring(0, 1));
		System.out.println("billTypeName="+billType);		
		
		String jasperFile ="";
	    if (ttl.equalsIgnoreCase("CA1")) {
	        if(allocateCode.equalsIgnoreCase("309")) {

	        	//jasperFile = "/ireport/cashGroupTTL1_309.jasper";
	        	jasperFile = "/ireport/cashGroupTTL1copy309.jasper";
	        	
	        }else if(allocateCode.equalsIgnoreCase("402")) {
	        	//jasperFile = "/ireport/cashGroupTTL1_402v376.jasper";
	        	System.out.println("402");
	        	jasperFile = "/ireport/cashGroupTTL1copy402.jasper";
	        }else {
	        	//jasperFile = "/ireport/cashGroupTTL1v3.jasper";
	        	jasperFile = "/ireport/cashGroupTTL1copy.jasper";
	        }
	     
	    }else {
	    	//jasperFile = "/ireport/cashGroupTTL2.jasper";
	    	jasperFile = "/ireport/cashGroupTTL2copy.jasper";
	    }
	    
		generateReport(jasperFile, hashMap, response, servlet);
		
	}
	
	@SuppressWarnings("unchecked")
	private void generateReport(String jasperFile, HashMap hashMap,
			HttpServletResponse response, ActionServlet servlet) throws Exception {

		if(CommonUtils.runReportToPdf(response, servlet, hashMap, jasperFile)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}					
	}

	public void showPettyCashSummaryReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String startDate 	  = mForm.get("startDate").toString();//.split("-");
		String endDate 	  = mForm.get("endDate").toString();//.split("-");
 		String	 ttl 		  = mForm.get("ttl").toString();
 		String	 remainMoney  = mForm.get("remainMoney").toString();	
 		
        try {
            DBUtil u = new DBUtil();
            u.insert(startDate, endDate, remainMoney, ttl);
                
        }catch (Exception e){   
//            this.SUCCESS = "error";
        } 
        
//	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
//	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
//	    	    
//	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
//	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
//	    
//	    DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
//						
//		HashMap hashMap = new HashMap();
//	    
//		hashMap.put("ttl",ttl);    
//		hashMap.put("startDate",sDate);
//		hashMap.put("endDate",eDate);    	    
//		hashMap.put("startDateH",(df.format(sDate2).toString()));
//		hashMap.put("endDateH",(df.format(eDate2).toString()));	    
//		hashMap.put("allocateCode",allocateCode);
//		hashMap.put("billType",billType.substring(0, 1));
//		hashMap.put("billTypeName",billType);
//		
//		System.out.println("ttl="+ttl);
//		System.out.println("startDate="+sDate);
//		System.out.println("endDate="+eDate);
//		System.out.println("startDateH="+(df.format(sDate2).toString()));
//		System.out.println("endDateH="+(df.format(eDate2).toString()));	    
//		System.out.println("allocateCode="+allocateCode);
//		System.out.println("billType="+billType.substring(0, 1));
//		System.out.println("billTypeName="+billType);		
//		
//		String jasperFile ="";
//	    if (ttl.equalsIgnoreCase("CA1")) {
//	        if(allocateCode.equalsIgnoreCase("309")) {
//
//	        	jasperFile = "/ireport/cashGroupTTL1_309.jasper";
//	        }else if(allocateCode.equalsIgnoreCase("402")) {
//	        	jasperFile = "/ireport/cashGroupTTL1_402.jasper";
//	        }else {
//	        	//jasperFile = "/ireport/cashGroupTTL1v3.jasper";
//	        	jasperFile = "/ireport/cashGroupTTL1copy.jasper";
//	        	System.out.println("aa");
//	        }
//	     
//	    }else {
//	    	jasperFile = "/ireport/cashGroupTTL2.jasper";
//	      
//	    }
//	    
//		generateReport(jasperFile, hashMap, response, servlet);		
	}	

}
